<template>
	<cl-crud ref="crud" @load="onLoad">
		<cl-pagefull>
			<template #head>
				<el-row type="flex">
					<cl-filter-group v-model="search">
						<cl-filter label="所属事业部">
							<cl-select
								v-model="search.biz_id"
								placeholder="请选择"
								filterable
								@change="changeBiz"
							>
								<el-option
									v-for="item in business"
									:key="item.biz_id"
									:label="item.biz_name"
									:value="item.biz_id"
								/>
							</cl-select>
						</cl-filter>
						<cl-filter label="所属品牌">
							<cl-select
								v-model="search.bra_id"
								placeholder="请选择"
								clearable
								filterable
							>
								<el-option label="全部" value="" />
								<el-option
									v-for="item in bizRefData.brand"
									:key="item.bra_id"
									:label="item.bra_name"
									:value="item.bra_id"
								/>
							</cl-select>
						</cl-filter>
						<cl-filter label="客服组名称">
							<el-input
								v-model="search.name"
								placeholder="请输入客服组名称"
								clearable
								style="width: 200px"
								@change="refresh"
							/>
						</cl-filter>
					</cl-filter-group>
				</el-row>
			</template>
			<el-row>
				<!-- 数据表格 -->
				<cl-table v-bind="table">
					<cl-empty slot="empty" />
					<template #column-cus_service="{ scope: { row } }">
						<span
							v-for="(item, index) in row.customer"
							:key="index"
							:class="{ 'color-danger': contactDisable(item) }"
						>
							{{ item.user_name }}({{ item.contact_type_desc }}:{{
								item.contact
							}},行为:{{ item.number || 0 }})
							<el-button type="text" @click="delCus(item, index, row.customer)"
								>移除;</el-button
							>
						</span>
					</template>
					<template #column-opt="{ scope: { row } }">
						<div class="flex flex-align-start flex-justify-center">
							<template>
								<!-- <el-button
									@click="openCusList(row)"
									type="primary"
									class="m-r-10"
									v-if="row.coprid"
									>客服列表</el-button
								> -->
								<el-button
									@click="openCusList(row)"
									type="primary"
									class="m-r-10"
									v-if="row.corpid"
									>客服列表</el-button
								>
								<el-button
									type="success"
									@click="openMiniProgram(row)"
									class="m-r-10"
									>绑定小程序</el-button
								>
								<el-button
									type="primary"
									plain
									:disabled="!$auth('store_landinglandinglist_edit')"
									@click="edit(row)"
									>修改</el-button
								>
							</template>
						</div>
					</template>
				</cl-table>
			</el-row>
			<template #foot>
			</template>
		</cl-pagefull>
		<bind-program-form ref="bindProgramForm" @confirm="refresh(false)" />
		<cus-list ref="cusList" @confirm="refresh(false)" />

	</cl-crud>
</template>

<script>
import BindProgramForm from "./bind-program-form";
import CusList from "./cus-list";


export default {
	name: "land-list",
	inject: ["parent"],
	components:{
		BindProgramForm,
		CusList
	},
	data() {
		return {
			visible: false,
			saving: false,
			poolList: [],
			poolForm: {
				id: "",
				wx_pool_id: "",
				bra_id: ""
			},
			poolRules: {
				bra_id: [{ required: true, message: "当前落地页id无绑定品牌" }]
			},
			// 表格配置
			table: {
				"context-menu": ["refresh"],
				columns: [
					{ label: "ID", prop: "id", width: 100, fixed: true },
					{
						label: "事业部",
						prop: "biz_name",
						minWidth: 100,
						align: "left",
						showOverflowTooltip: true
					},
					{ label: "品牌", prop: "bra_name", minWidth: 150, align: "left" },
					{
						label: "名称",
						prop: "name",
						minWidth: 150,
						align: "left",
						showOverflowTooltip: true
					},
					{ label: "类型", prop: "multiple_customer_link", align: "left", minWidth: 200, formatter: row => row.multiple_customer_link == 1 ? '非去重' : '去重' },
					{
						label: "客服码类型",
						prop: "contact_type_name",
						align: "center",
						minWidth: 120
					},
					{ label: "操作", prop: "opt", width: 280, fixed: "right" }
				]
			},
			search: {
				name: "",
				biz_id: "",
				bra_id: "",
				type: ""
			},
			brandList: []
		};
	},
	computed: {
		bizRefData() {
			const { biz_id } = this.search;
			return this.getBizRefData(biz_id);
		}
	},
	created() {
		this.search.biz_id = this.getCurrBiz();
		// this.getBrandList();
		this.init();
	},
	methods: {
		async init() {
			const { biz_id } = this.search;
			this.getBizRefBrand(biz_id);
		},
		onLoad({ ctx, app }) {
			ctx.service(this.$service.wx).done();
			app.refresh(this.search);
		},
		// crud刷新
		refresh(reset = true) {
			let params = { ...this.search };
			if (reset) {
				params.page = 1;
			}
			this.$refs.crud.refresh(params);
		},
		changeBiz() {
			this.search.bra_id = "";
			this.getBrand();
			this.refresh();
		},
		//获取品牌
		async getBrand() {
			const { biz_id } = this.search;
			const data = await this.getBizRefBrand(biz_id);
			this.search.bra_id = data.length == 1 ? data[0]?.bra_id ?? "" : "";
		},
		contactDisable(item) {
			return item.ad_close_date != 1 || item.banci != 0;
		},
		// 移除客服
		async delCus(item, index, list) {
			this.confirm()
				.then(async () => {
					let params = {
						...item,
						type: 4
					};
					await this.$service.ad.cusDel(params);
					this.$message.success("操作成功");
					// this.refresh();

					list.splice(index, 1);
				})
				.catch(() => {});
		},
		//修改
		edit(row) {
			this.parent.openFlowPoolForm(true, row);
		},
		// 删除
		remove({ id }) {
			this.confirm()
				.then(async () => {
					await this.$service.ad.remove({ id });
					this.$message.success("操作成功");
					this.refresh();
				})
				.catch(() => {});
		},
		openMiniProgram(item) {
			this.$refs.bindProgramForm.open(item)
		},
		openCusList(item){
			this.$refs.cusList.open(item)

		}


	}
};
</script>
<style lang="scss" scoped>
.type_color_normal {
	text-align: center;
	color: #156DFF;
	width: 40%;
	/* height: 26px; */
	border: 0.5px solid;
	margin: auto;
}
.type_color_flowPool {
	text-align: center;
	color: #18eca2;
	width: 40%;
	/* height: 26px; */
	border: 0.5px solid;
	margin: auto;
}
.type_color_bule {
	text-align: center;
	color: #f56c6c;
	width: 60%;
	/* height: 26px; */
	border: 0.5px solid;
	margin: auto;
}
</style>
